<?php

include 'seguranca.php';
include 'Model/DAO/ConnectionPDO.php';
include 'Model/DAO/AnimalDAO.php';
require_once 'Model/DAO/FiliacaoDAO.php';
require_once 'Model/Filiacao.php';
include 'Model/Animal.php';
protegePagina();

$animalDAO = new AnimalDAO();

if(isset($_POST['edtAnimal']))
{
  $animal = new Animal();
  $animal->setIdAnimal($_GET['IDFiliacao']);
  $animal->setNomeAnimal($_POST['nome']);
  $animal->setFoto($_FILES['foto']['tmp_name']);
  $animal->setRaca($_POST['cboRaca']);
  $animal->setTipoAnimal($_POST['cboTipo']);
  $animal->setSexoAnimal($_POST['sexo']);
  $animal->setPelagemAnimal($_POST['pelagem']);
  $animal->setDataNasc($_POST['nasc']);
  $animal->setMunicipio($_POST['municipio']);
  $animal->setPeso($_POST['peso']);
  $animal->setFiliacao($_POST['cboFiliacao']);

  $animalDAO->updateFiliacao($animal);

  if($animalDAO) {
    $msg = 'Registro alterado com sucesso';
    header('Location: animal_list.php');
  } else  {
   $msg = 'Não foi possivel alterar o registro,tente novamente';
   header('Location: animal_edit.php');
  }
}
$showAnimal = $animalDAO->getByIDAnimal($_GET['IDAnimal']);
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='pt-br' lang='pt-br'>
    <head>
        <meta http-equiv='content-type' content='text/html; charset=utf-8' />
        <meta http-equiv='content-language' content='pt-br' />
        <meta name='robots' content='noindex,nofollow' />
        <link rel='stylesheet' media='screen,projection' type='text/css' href='css/reset.css' />
        <link rel='stylesheet' media='screen,projection' type='text/css' href='css/main.css' />
        <link rel='stylesheet' media='screen,projection' type='text/css' href='css/2col.css' title='2col' />
        <link rel='alternate stylesheet' media='screen,projection' type='text/css' href='css/1col.css' title='1col' />
        <!--[if lte IE 6]><link rel='stylesheet' media='screen,projection' type='text/css' href='css/main-ie6.css' /><![endif]--> <!-- MSIE6 -->
        <link rel='stylesheet' media='screen,projection' type='text/css' href='css/style.css' />
        <link rel='stylesheet' media='screen,projection' type='text/css' href='css/mystyle.css' />
        <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
        <script type='text/javascript' src='http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js'></script>
        <script type='text/javascript' src='js/maskedinput.js'></script>
        <script type='text/javascript'>
            $(document).ready(function(){
                $('#nasc').mask('99/99/9999');
                $('#frmAnimal').validate({

                    // Reporta os erros no container
                    errorLabelContainer: '#error',
                    wrapper: 'li',
                    errorClass: 'campo-invalido',

                    rules: {
                        nome:{
                            required: true,
                            minlength: 3
                        },
                        foto: {
                            required: true,
                            accept:'jpg|png|gif'
                        },
                        cboTipo:'required: true',
                        cboRaca:'required: true',
                        cboFiliacao:'required: true',
                        nasc:'required: true',
                        peso:'required:true',
                        municipio:'required:true'
                    },
                    messages: {
                        nome:{
                            required: 'Informe o <strong>NOME</strong>',
                            minlength:'O <strong>NOME</strong> deve ser maior que 3 caracteres'
                        },
                        foto: {
                            required: 'Informe a <strong>FOTO</strong> dp animal',
                            minlength:'A <strong>FOTO</strong> deve ser nas extensões <strong>JPG,GIF ou PNG</strong>'
                        },
                        cboTipo:{ required: 'Informe o <strong>TIPO</strong> de animal'
                        },
                        cboRaca:{required: 'Informe a <strong>RAÇA</strong>'},
                        nasc:{
                            required: 'Informe a <strong>DATA DE NASCIMENTO</strong> ex: 99/99/9999'
                        },
                        peso: 'Informe o <strong>PESO</strong> do animal',
                        municipio: 'Informe o <strong>MUNÍCIPIO</strong> de nascimento do animal'
                    }
                });
            });

        </script>
        <title>Animal</title>
    </head>
    <body>
        <div id='main'>
<?php include 'includes/inc_header.php' ?>
<?php include 'includes/inc_menu.php' ?>
            <div id='cols' class='box'><?php include 'includes/left-colum-cad.php' ?></div>

            <div id='content' class='box'>
                <h1>Alteração de animais</h1>

                <p class='msg info' id='resposta'><?=$msg?></p>
                <p class='msg error' id='error'></p>
                <form action='' method='post' id='frmAnimal' enctype='multipart/form-data'>
                    <fieldset>
                        <legend>Animal</legend>
                        <p class='nomt'>
                            <label for='nome' class='req'>Nome do Animal: *</label><br />
                            <input type='text'
                                   size='60'
                                   name='nome'
                                   class='input-text required'
                                   id='nome'
                                   value='<?=$showAnimal->getNomeAnimal()?>'
                                   title="Preencha o nome do animal"  />
                        </p>
                        <p class='nomt'>
                            <label for='foto' class='req'>Foto Animal: *</label><br />
                            <input type='file'
                                   size='60'
                                   name='foto'
                                   class='input-text required'
                                   id='foto'
                                   value='<?=$showAnimal->getFoto()?>'
                                   title='Escolha uma foto'  />
                        </p>
                        <p class='nomt'>
                            <label for='pelagem' class='req'>Pelagem:</label><br />
                            <input type='text'
                                   size='60'
                                   name='pelagem'
                                   value='<?=$showAnimal->getPelagemAnimal()?>'
                                   class='input-text required'
                                   id='pelagem' />
                        </p>
                        <p class='nomt'>
                            <label for='cboTipo' class='req'>Tipo: *</label><br/>
                            <select name='cboTipo' id='cboTipo'>
                            <?php
                              $animal = new Animal();
                              $animalDAO = new AnimalDAO();
                              foreach ($animalDAO->getAllAnimal() as $linha) {
                            ?>
                            <option value="<?=$showAnimal->getTipoAnimal()?>"
                            <?= $animal->getTipoAnimal() == $linha["tipoAnimal"]; ?>>
                            <?=$linha['tipoAnimal']?></option>
                            <option value='Matrizes'>Matrizes</option>
                            <option value='Cavalos'>Cavalos</option>
                            </select>
                            <?php } ?>
                        </p>

                        <p class='nomt'>
                            <label for='cboRaca' class='req'>Raça: *</label><br />
                            <select name='cboRaca' id='cboRaca'>
                            <?php
                              $animal = new Animal();
                              $animalDAO = new AnimalDAO();
                              foreach ($animalDAO->getAllAnimal() as $linha) {
                            ?>

                            <option value="<?=$showAnimal->getRaca()?>">
                              <?php if($showAnimal->getRaca() == $linha['raca'])'selected=true'; ?><?=utf8_encode($linha['raca'])?>
                            </option>
                            <option value='Gir Leiteiro'>Gir Leiteiro</option>
                            <option value='Simental'>Simental</option>
                            <option value='Pampa'>Pampa</option>
                            </select>
                            <?php } ?>
                        </p>

                        <p class='nomt'>
                            <label for='sexo' class='req'>Sexo: *</label><br />
                            <input type='radio' 
                                   name='sexo'
                                   id='sexo'
                                   <?php if($showAnimal->getSexoAnimal() =='Macho')'selected=true'; ?>
                                   value="<?=$showAnimal->getSexoAnimal()?>" /> Macho

                            <input type='radio' name='sexo' id='sexo' value='Fêmea' /> Fêmea
                        </p>
                        <p class='nomt'>
                            <label for='nasc' class='req'>Data de Nascimento: *</label><br />
                            <input type='text'
                                   name='nasc'
                                   class='input-text required'
                                   value='<?=$showAnimal->getDataNasc()?>'
                                   id='nasc'
                                   size='50'  />
                        </p>
                        <p class='nomt'>
                            <label for='peso' class='req'>Peso:</label><br />
                            <input type='text'
                                   name='peso'
                                   class='input-text required'
                                   value='<?=$showAnimal->getPeso()?>'
                                   id='peso'
                                   size='30'  />
                        </p>
                        <p class='nomt'>
                            <label for='municipio' class='req'>Municipio:</label><br />
                            <input type='text'
                                   name='municipio'
                                   class='input-text required'
                                   id='municipio'
                                   value='<?=$showAnimal->getMunicipio()?>'
                                   size='30' />
                        </p>
                    </fieldset>
                    <fieldset>
                        <legend>Filiação</legend>

                        <p class='nomt'>
                            <label for='cboFiliacao' class='req'>Filiação: *</label><br />
                            <select name="cboFiliacao" id="cboFiliacao">
                            <?php
                              $animal = new Filiacao();
                              $animalDAO = new FiliacaoDAO();
                              foreach ($animalDAO->getAll() as $linhas) {
                            ?>
                            <option value="<?= $linhas['IDFiliacao']; ?>"
                              <?= $animal->getIdFiliacao() == $linhas["IDFiliacao"]; ?>>
                              <?= $linhas['pai'] . ' e ' . $linhas['mae']; ?></option>
                            <?php } ?>
                            </select> <a href="filiacao_cad.php" target="blank" title="Adicionar filiação"> Adiciona filiacao[+]</a>
                        </p>

                    </fieldset>
                    <p><input type='submit' name="edtAnimal" id="enviaAnimal" value='Cadastrar Animal' /></p>
                </form>
            </div>
        </div>
        <hr class='noscreen' />
<?php include 'includes/inc_footer.php'; ?>
